/*
 * test.java
 *
 * 
 */
package it.luigiarpino.alt2jsim.netpanel.nlevel;

import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Rectangle;
import java.lang.Math;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.*;
import java.util.Vector;

/**
 *
 * @author  Luigi Lorenzo Arpino 534/559
 */
public class CircolarPanel extends JPanel {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 9013915363011798251L;
	private int[][] link;
    private String[] nomiNodi;
    private JPanel jPanelSouthPanel;
    private JPanel jPanelNorthPanel;
    private JComboBox jComboBoxSelectNode;
    private int nodoSelezionato=0;
    private BufferedImage bufferedImageNodo;
    
     /**Mostra nodi come reti di terzo livello*/
    public static final int LEVEL2NET=2;
    /** Mostra nodi come reti di secondo livello */
    public static final int LEVEL1NET=1;
    /**Mostra nodi router*/
    public static final int ROUTER=3;
    
    /** Creates a new instance  */
    public CircolarPanel(int[][] link_, String[] nomiNodi_,String titolo,String seleziona,int tipoDiNodo) {
        super();
        try { 
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());     
    	} 
    	catch(Exception e) { }
        this.link=link_;
        this.nomiNodi=nomiNodi_;
        this.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mouseClick(evt);
            }
        });
        this.setLayout(new BorderLayout());
        this.jPanelSouthPanel=new JPanel(new FlowLayout());
        String[] modello=new String[this.nomiNodi.length];
        for(int i=0;i<modello.length;i++){
            modello[i]="  "+this.nomiNodi[i]+"  ";
        }
        this.jComboBoxSelectNode=new JComboBox(new DefaultComboBoxModel(modello));
        this.jComboBoxSelectNode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ComboActionPerformed(evt);
            }
        });
        this.jPanelSouthPanel.add(new JLabel(seleziona));
        this.jPanelSouthPanel.add(this.jComboBoxSelectNode);
        this.jPanelNorthPanel=new JPanel(new FlowLayout());
        this.jPanelNorthPanel.add(new JLabel(titolo));
        this.add(this.jPanelNorthPanel,BorderLayout.NORTH);
        this.add(this.jPanelSouthPanel,BorderLayout.SOUTH);
        // carico le immagini
        try{
            java.net.URL u;
            if (tipoDiNodo== CircolarPanel.LEVEL1NET){
                u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/level2netBlu.gif");
                this.bufferedImageNodo= ImageIO.read(u);
            }
            if (tipoDiNodo==CircolarPanel.LEVEL2NET){
                // carica i nodi come reti di terzo livello
                u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/level3netBlu.gif");
                this.bufferedImageNodo= ImageIO.read(u);
            }
            if (tipoDiNodo==CircolarPanel.ROUTER){
                u = this.getClass().getResource("/it/luigiarpino/alt2jsim/images/routerBlu.gif");
                this.bufferedImageNodo= ImageIO.read(u);
            }
        }
        catch(java.lang.Exception e){}
        //Rendo la matrice simmetrica
        for(int i=0;i<this.link.length;i++){
            for(int j=0;j<this.link.length;j++){
                if (this.link[i][j]!=0){
                    this.link[j][i]=1;
                }
            }
        }
    }
    
    /**L'utente seleziona un nodo*/
    private void ComboActionPerformed(java.awt.event.ActionEvent evt) {
        this.nodoSelezionato=this.jComboBoxSelectNode.getSelectedIndex();
        this.repaint();
    }
    
    /** l'utente clicca sul pannello */    
    private void mouseClick(java.awt.event.MouseEvent evt){
        
    }
    
    
     public void paintComponent(Graphics g) {
         Rectangle r=new Rectangle(this.getBounds());
         g.clearRect(0,0,r.width ,r.height);
         g.setColor(Color.BLACK);
         //Genero l'elenco dei nodi
         Vector vett=new Vector();
         for(int i=0;i<this.link.length;i++){
             if (this.link[this.nodoSelezionato][i]!=0){
                 vett.add(this.nomiNodi[i]);
             } 
         }
         vett.add(0, this.nomiNodi[this.nodoSelezionato]);
         //Disegno i link
         for(int i=0;i<vett.size();i++){
             double angolo1=4.71;
             double angolo2=(6.28/vett.size())*i+4.71;
             int x1=(int)(Math.cos(angolo1)*(r.width*0.35)+(r.width/2));
             int y1=(int)(Math.sin(angolo1)*(r.height*0.35)+(r.height/2));
             int x2=(int)(Math.cos(angolo2)*(r.width*0.35)+(r.width/2));
             int y2=(int)(Math.sin(angolo2)*(r.height*0.35)+(r.height/2));
             g.drawLine(x1,y1,x2,y2);
         }
         //Disegno i nodi
         for(int i=0;i<vett.size();i++){
             double angolo=(6.28/vett.size())*i+4.71;
             int x=(int)(Math.cos(angolo)*(r.width*0.35)+(r.width/2));
             int y=(int)(Math.sin(angolo)*(r.height*0.35)+(r.height/2));
             try{
                   Graphics2D g2 = (Graphics2D) g;
                   g2.drawImage(this.bufferedImageNodo,null,x-18,y-13 );
               }
             catch(java.lang.Exception e){
                   int raggio=5;
                   g.fillOval(x-raggio, y-raggio, 2*raggio,2*raggio);
             }
         }// Ho finoto di disegnare i nodi
         //Nomino i nodi
         for (int i=0;i<vett.size();i++){
             double angolo=(6.28/vett.size())*i+4.71;
             int x=(int)(Math.cos(angolo)*(r.width*0.42)+(r.width/2));
             int y=(int)(Math.sin(angolo)*(r.height*0.42)+(r.height/2));
             g.drawString((String)vett.get(i), x, y);
         }
     }//fine paintComponent()
     
     
     public static void main(String[] args){
         
     }
  
}//fine classe

